package com.loong.musicdemo.ui.mine.recent.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.loong.musicdemo.R;
import com.loong.musicdemo.model.MV;
import com.loong.musicdemo.ui.mine.adapter.MineMvAdapter;
import com.loong.musicdemo.util.dataDeal.MvDeal;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class RecentMvFragment extends Fragment {
    private static List<String> mvIds = new ArrayList<>();
    private RecyclerView recent_mvs_view;
    private ProgressBar progressBar;
    private TextView mv_none_tip;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_recent_mv, container, false);
        recent_mvs_view = root.findViewById(R.id.recent_mvs_view);
        progressBar = root.findViewById(R.id.progress_bar);
        mv_none_tip = root.findViewById(R.id.mv_none_tip);
        mv_none_tip.setVisibility(View.GONE);
        getMvIds();
        new MvTask().execute();
        return root;
    }
    private void getMvIds(){
        SharedPreferences ids = getActivity().getSharedPreferences("mvIds", Activity.MODE_PRIVATE);
        int size = ids.getInt("size",0);
        if(size!=0) {
            for (int i = 0; i < size; i++) {
                String id = ids.getString("mvId_" + i, null);
                mvIds.add(id);
            }
        }else {
            progressBar.setVisibility(View.GONE);
            recent_mvs_view.setVisibility(View.GONE);
            mv_none_tip.setText("亲！当前无数据");
            mv_none_tip.setVisibility(View.VISIBLE);
        }
    }
    class MvTask extends AsyncTask<MV,Void,List<MV>>{

        @Override
        protected List<MV> doInBackground(MV... mvs) {
            List<MV> mvList = new ArrayList<>();
            mvList = LitePal.findAll(MV.class);
            if(mvList.size() != 0){
                return mvList;
            }else {
                for(int i=0;i<mvIds.size();i++ ){
                    mvList.add(MvDeal.getMVDetailByMVId(mvIds.get(i)));
                }
                return mvList;
            }
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(List<MV> mvs) {
            super.onPostExecute(mvs);
            LinearLayoutManager manager = new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false);
            MineMvAdapter adapter = new MineMvAdapter(mvs,getContext());
            recent_mvs_view.setLayoutManager(manager);
            recent_mvs_view.setAdapter(adapter);
            progressBar.setVisibility(View.GONE);
        }
    }
}
